package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.MyCollectNote;
import com.lsy.weizhi2.JsonObject.MyJoin;
import com.lsy.weizhi2.JsonObject.NoteList;
import com.lsy.weizhi2.JsonObject.WoYueJson;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.ActivityDetail;
import com.lsy.weizhi2.activity.Appraise;
import com.lsy.weizhi2.activity.NoteDetail;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 嗨哟 on 2017/3/28.
 */
public class ManagerAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<NoteList.ListBean> list;
    private List<WoYueJson.ListBean> list_wo;
    private List<MyJoin.ListBean>list_join;

    private LayoutInflater layoutInflater;
    private int flag;//临时识别--区分我发布的还是我参与的
    private int tag;//区别微职和约单加载不同的item
    private int bottom_tag;//加载显示
    private boolean bottom;
    private int bottom_count;

    public ManagerAdapter(Context context, List<NoteList.ListBean> list,int flag) {
        this.context = context;
        this.list = list;
        this.flag=flag;
        layoutInflater=LayoutInflater.from(context);
    }

    public ManagerAdapter(Context context,int flag,int tag) {
        this.context = context;
        this.flag=flag;
        this.tag=tag;
        layoutInflater=LayoutInflater.from(context);
    }

    public void UpData(int tag,List<NoteList.ListBean> list,int bottom_count,int bottom_tag){
        this.tag=tag;
        this.list=list;
        this.bottom_tag=bottom_tag;
        this.bottom_count=bottom_count;
        this.notifyDataSetChanged();
    }

    public void UpData2(int tag, List<WoYueJson.ListBean> list_wo){
        this.tag=tag;
        this.list_wo=list_wo;
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=layoutInflater.inflate(R.layout.manager_item,parent,false);
        View view_yue=layoutInflater.inflate(R.layout.manager_item_yue,parent,false);
        View view_weizhi=layoutInflater.inflate(R.layout.manager_item_weizhi,parent,false);
        View view_activity=layoutInflater.inflate(R.layout.activity_list_item,parent,false);
        View view_ask_other = layoutInflater.inflate(R.layout.manager_item_i_ask,parent,false);
        View view_bottom = layoutInflater.inflate(R.layout.bottom_view,parent,false);
        AutoUtils.autoSize(view);
        AutoUtils.autoSize(view_yue);
        AutoUtils.autoSize(view_weizhi);
        AutoUtils.autoSize(view_activity);
        AutoUtils.autoSize(view_ask_other);
        AutoUtils.autoSize(view_bottom);
        if(viewType==ManagerItem.Bottom){
            return new BottomHolder(view_bottom);
        }else if(tag== ManagerItem.WeiZhi){
            return new ManagerViewHolder(view);
        }else if(tag==ManagerItem.YueDan){
            return new YueDanViewHolder(view_yue);
        }else if(tag==ManagerItem.CollectActivity){
            return new ActivityViewHolder(view_activity);
        }else if(tag==ManagerItem.CollectWeiZhi){
            return new WeiZhiViewHolder(view_weizhi);
        }else if(tag==ManagerItem.Conclude){
            return new ConcludeViewHolder(view_ask_other);
        }else {
            return null;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ManagerViewHolder){
            ((ManagerViewHolder) holder).position=position;
            if(flag==0){
                ((ManagerViewHolder) holder).manager_name.setVisibility(View.GONE);
            }
            if(list!=null&&list.size()!=0){
                ((ManagerViewHolder) holder).manager_title.setText(list.get(position).getTitle());
                ((ManagerViewHolder) holder).manager_time.setText(list.get(position).getCreateTime());
                ((ManagerViewHolder) holder).manager_num.setText("已成交"+list.get(position).getDealCount()+"次");
                if(list.get(position).getPriceMethod()==0){//次
                    ((ManagerViewHolder) holder).manager_price.setText(list.get(position).getPrice()+"/次");//服务单价
                }else if(list.get(position).getPriceMethod()==1){//时
                    ((ManagerViewHolder) holder).manager_price.setText(list.get(position).getPrice()+"/时");//服务单价
                }else if(list.get(position).getPriceMethod()==2){//天
                    ((ManagerViewHolder) holder).manager_price.setText(list.get(position).getPrice()+"/天");//服务单价
                }
            }
        }else if(holder instanceof YueDanViewHolder){
            ((YueDanViewHolder) holder).position=position;
            if(flag==0){
                ((YueDanViewHolder) holder).yue_dan_cancel.setText("取消预约");
                ((YueDanViewHolder) holder).yue_dan_state.setText("预约中");
            }else{
                ((YueDanViewHolder) holder).yue_dan_cancel.setText("拒绝");
                ((YueDanViewHolder) holder).yue_dan_state.setText("接单");
            }
            if(list_wo!=null&&list_wo.size()!=0){
                ((YueDanViewHolder) holder).yue_dan_name.setText(list_wo.get(position).getServiceProvider().getRealName());
                ((YueDanViewHolder) holder).yue_dan_main.setText(list_wo.get(position).getOrder().getTitle());
                ((YueDanViewHolder) holder).yue_dan_words.setText(list_wo.get(position).getOrder().getContent());
                ((YueDanViewHolder) holder).yue_dan_time.setText(list_wo.get(position).getCreateTime());
                int status=list_wo.get(position).getStatus();
                if(status==0&&flag==0){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("预约中");
                }else if(status==-5){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("已拒绝");
                }else if(status==20){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("已接单");
                }else if(status==30){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("已完成");
                }else if(status==40){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("已评价");
                }else if(status==-10){
                    ((YueDanViewHolder) holder).yue_dan_state.setText("已取消");
                }
                Glide.with(context).load(Constants.DOWN_USER_PICTURE+list_wo.get(position).getServiceProvider().getAvatar())
                        .error(R.mipmap.default_head).into(((YueDanViewHolder) holder).yue_dan_head_image);
            }

        }else if(holder instanceof ActivityViewHolder){
            ((ActivityViewHolder) holder).position=position;
        }else if(holder instanceof WeiZhiViewHolder){

        }else if(holder instanceof BottomHolder){
            if(bottom_count==1&&bottom_tag==0){
                ((MyCollectAdapter.BottomHolder) holder).bottom_tv.setText("松开加载");
            }else if(bottom_count==1&&bottom_tag==1){
                ((MyCollectAdapter.BottomHolder) holder).bottom_tv.setText("正在加载");
            }else if(bottom_count==1&&bottom_tag==2){
                ((MyCollectAdapter.BottomHolder) holder).bottom_tv.setText("到底了");
            }
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(list!=null&&position==list.size()){
            return ManagerItem.Bottom;
        }else if(list_join!=null&&position==list_join.size()){
            return ManagerItem.Bottom;
        }else if(list_wo!=null&&position==list_wo.size()){
            return ManagerItem.Bottom;
        }else{
            return super.getItemViewType(position);
        }

    }

    @Override
    public int getItemCount() {
        if(tag==ManagerItem.YueDan){
            return list_wo==null?0+bottom_count:list_wo.size()+bottom_count;
        }else if(tag==ManagerItem.WeiZhi&&flag==0){
            return list==null?0+bottom_count:list.size()+bottom_count;
        }else if(tag==ManagerItem.WeiZhi&&flag==1){
            return list_join==null?0+bottom_count:list_join.size()+bottom_count;
        }else{
            return 0;
        }
    }

    class ManagerViewHolder extends RecyclerView.ViewHolder{
        int position;
        RelativeLayout manager_item_view;
        TextView manager_title,manager_classify,manager_time,manager_price,manager_num,manager_name;
        public ManagerViewHolder(View itemView) {
            super(itemView);
            manager_item_view= (RelativeLayout) itemView.findViewById(R.id.manager_item_view);
            manager_title= (TextView) itemView.findViewById(R.id.manager_title);
            manager_classify= (TextView) itemView.findViewById(R.id.manager_classify);
            manager_time= (TextView) itemView.findViewById(R.id.manager_time);
            manager_price= (TextView) itemView.findViewById(R.id.manager_price);
            manager_num= (TextView) itemView.findViewById(R.id.manager_num);
            manager_name= (TextView) itemView.findViewById(R.id.manager_name);
            manager_item_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Gson gson=new Gson();
                    if(list!=null&&list.get(position)!=null){
                        String json=gson.toJson(list.get(position));
                        MyMethod.toActivity(context,NoteDetail.class,new String[]{"json","tag"},new String[]{json,ManagerItem.WeiZhi+""});
                    }else if(list_join!=null&&list_join.get(position)!=null){
                        String json=gson.toJson(list_join.get(position));
                        MyMethod.toActivity(context,NoteDetail.class,new String[]{"json","tag"},new String[]{json,ManagerItem.CollectWeiZhi+""});
                    }
                }
            });
            manager_item_view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    Log.i("ManagerAdapter","删除");
                    new AlertDialog.Builder(context).setMessage("是否删除").setNegativeButton("删除", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            MyOkHttpUtils.getInstance(context).doDetele(Constants.ShouCang+"/"+list.get(position).getId())
                                    .excute(new MyOkHttpUtils.OKCallBack() {
                                        @Override
                                        public void onFailure(String message) {

                                        }

                                        @Override
                                        public void onResponse(String message) {
                                            Log.i("ManagerAdapter","message==delete=="+message);
                                            if(message!=null){
                                                try {
                                                    JSONObject jsonObject=new JSONObject(message);
                                                    boolean result=jsonObject.getBoolean("result");
                                                    if(result){
                                                        list.remove(position);
                                                        UpData(ManagerItem.WeiZhi,list,0,0);
                                                    }
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    });
                        }
                    }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).show();
                    return false;
                }
            });
        }
    }

    class YueDanViewHolder extends RecyclerView.ViewHolder{
        int position;
        ImageView yue_dan_head_image;
        TextView yue_dan_name,yue_dan_time,yue_dan_renzheng,
                yue_dan_main,yue_dan_words,yue_dan_state,yue_dan_cancel;
        public YueDanViewHolder(View itemView) {
            super(itemView);
            yue_dan_head_image= (ImageView) itemView.findViewById(R.id.yue_dan_head_image);
            yue_dan_name= (TextView) itemView.findViewById(R.id.yue_dan_name);
            yue_dan_time= (TextView) itemView.findViewById(R.id.yue_dan_time);
            yue_dan_renzheng= (TextView) itemView.findViewById(R.id.yue_dan_renzheng);
            yue_dan_main= (TextView) itemView.findViewById(R.id.yue_dan_main);
            yue_dan_words= (TextView) itemView.findViewById(R.id.yue_dan_words);
            yue_dan_state= (TextView) itemView.findViewById(R.id.yue_dan_state);
            yue_dan_cancel= (TextView) itemView.findViewById(R.id.yue_dan_cancel);
            yue_dan_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch(yue_dan_cancel.getText().toString()){
                        case "取消预约":
                            CancelYueDan(list_wo.get(position).getId()+"",position);
                            break;
                        case "拒绝":
                            Refuse(list_wo.get(position).getId()+"",position);
                            break;
                    }

                }
            });
            yue_dan_state.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (yue_dan_state.getText().toString()){
                        case "接单":
                            Log.i("ManagerAdapter","接单"+list_wo.get(position).getOrder().getId());
                            JieDan(list_wo.get(position).getId()+"",position);
                            break;

                    }
                }
            });
        }
    }

    class ActivityViewHolder extends RecyclerView.ViewHolder{
        int position;
        TextView activity_title,activity_classify,activity_num,activity_time,activity_name;
        RelativeLayout activity_item_view;
        public ActivityViewHolder(View itemView) {
            super(itemView);
            activity_item_view= (RelativeLayout) itemView.findViewById(R.id.activity_item_view);
            activity_title= (TextView) itemView.findViewById(R.id.activity_title);
            activity_classify= (TextView) itemView.findViewById(R.id.activity_classify);
            activity_num= (TextView) itemView.findViewById(R.id.activity_num);
            activity_time= (TextView) itemView.findViewById(R.id.activity_time);
            activity_name= (TextView) itemView.findViewById(R.id.activity_name);
            activity_item_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MyMethod.toActivity(context, ActivityDetail.class);
                }
            });
        }
    }

    class WeiZhiViewHolder extends RecyclerView.ViewHolder{
        int position;
        TextView weizhi_title,weizhi_classify,weizhi_num,weizhi_time,weizhi_name,weizhi_price;
        RelativeLayout weizhi_itme_view;
        public WeiZhiViewHolder(View itemView) {
            super(itemView);
            weizhi_itme_view= (RelativeLayout) itemView.findViewById(R.id.weizhi_itme_view);
            weizhi_title= (TextView) itemView.findViewById(R.id.weizhi_title);
            weizhi_classify= (TextView) itemView.findViewById(R.id.weizhi_classify);
            weizhi_num= (TextView) itemView.findViewById(R.id.weizhi_num);
            weizhi_time= (TextView) itemView.findViewById(R.id.weizhi_time);
            weizhi_name= (TextView) itemView.findViewById(R.id.weizhi_name);
            weizhi_price= (TextView) itemView.findViewById(R.id.weizhi_price);
            weizhi_itme_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Gson gson=new Gson();
                    String json=gson.toJson(list.get(position));
                    MyMethod.toActivity(context,NoteDetail.class,new String[]{"json"},new String[]{json});
                }
            });
        }
    }

    class ConcludeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        int position;
        ImageView yue_dan_head_image,yue_dan_siliao_iv,yue_dan_call_iv;
        TextView yue_dan_name,yue_dan_time,yue_dan_renzheng,
                yue_dan_main,yue_dan_words,yue_dan_state,yue_dan_cancel;
        public ConcludeViewHolder(View itemView) {
            super(itemView);
            yue_dan_head_image= (ImageView) itemView.findViewById(R.id.yue_dan_head_image);
            yue_dan_siliao_iv= (ImageView) itemView.findViewById(R.id.yue_dan_siliao_iv);
            yue_dan_call_iv= (ImageView) itemView.findViewById(R.id.yue_dan_call_iv);
            yue_dan_name= (TextView) itemView.findViewById(R.id.yue_dan_name);
            yue_dan_time= (TextView) itemView.findViewById(R.id.yue_dan_time);
            yue_dan_renzheng= (TextView) itemView.findViewById(R.id.yue_dan_renzheng);
            yue_dan_main= (TextView) itemView.findViewById(R.id.yue_dan_main);
            yue_dan_words= (TextView) itemView.findViewById(R.id.yue_dan_words);
            yue_dan_state= (TextView) itemView.findViewById(R.id.yue_dan_state);
            yue_dan_cancel= (TextView) itemView.findViewById(R.id.yue_dan_cancel);
            yue_dan_state.setOnClickListener(this);
            yue_dan_cancel.setOnClickListener(this);
            yue_dan_siliao_iv.setOnClickListener(this);
            yue_dan_call_iv.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.yue_dan_state:
                    MyMethod.toActivity(context, Appraise.class);
                    break;
                case R.id.yue_dan_cancel:
                    break;
                case R.id.yue_dan_siliao_iv:
                    break;
                case R.id.yue_dan_call_iv:
                    Intent intent=new Intent();
                    intent.setAction(Intent.ACTION_CALL);
                    //url:统一资源定位符
                    //uri:统一资源标示符（更广）
                    intent.setData(Uri.parse("tel:" + "13006335638"));
                    //开启系统拨号器
                    context.startActivity(intent);
                    break;
            }
        }
    }

    private void JieDan(String orderId, final int position){
        Log.i("ManagerAdapter","接单==="+Constants.JieDan+orderId+"/accept");
        Map<String,String> map=new HashMap<>();
        map.put("orderId",orderId+"");
        MyOkHttpUtils.getInstance(context).doPut(Constants.JieDan+orderId+"/accept",map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {
                        Log.i("ManagerAdapter","message==fff=="+message);
                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("ManagerAdapter","message==ss=="+message);
                        try {
                            JSONObject jsonObject=new JSONObject(message);
                            boolean result=jsonObject.getBoolean("result");
                            if(result){
                                list_wo.get(position).getOrder().setStatus(jsonObject.getJSONObject("data").getInt("status"));
                                notifyItemChanged(position);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void Refuse(String orderId, final int position){
        MyOkHttpUtils.getInstance(context).doPut(Constants.JuJue+orderId+"/refuse",null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {
                        Log.i("ManagerAdapter","message==fff=="+message);
                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("ManagerAdapter","message=拒绝=="+message);
                        if(message!=null){
                            try {
                                JSONObject jsonObject=new JSONObject(message);
                                boolean result=jsonObject.getBoolean("result");
                                if(result){
                                    list_wo.get(position).getOrder().setStatus(jsonObject.getJSONObject("data").getInt("status"));
                                    notifyItemChanged(position);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    private void CancelYueDan(String orderId, final int position){
        MyOkHttpUtils.getInstance(context).doPut(Constants.QuXiao+orderId+"/cancel",null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("ManagerAdapter","message=取消=="+message);
                        if(message!=null){
                            try {
                                JSONObject jsonObject=new JSONObject(message);
                                boolean result=jsonObject.getBoolean("result");
                                if(result){
                                    list_wo.get(position).getOrder().setStatus(jsonObject.getJSONObject("data").getInt("status"));
                                    notifyItemChanged(position);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                });
    }

    class BottomHolder extends RecyclerView.ViewHolder{
        TextView bottom_tv;
        public BottomHolder(View itemView) {
            super(itemView);
            bottom_tv= (TextView) itemView.findViewById(R.id.bottom_tv);
        }
    }
}
